1. 题目描述(中等难度)

[warning] 131. 分割回文串

class Solution {
    List<List<String>> resp = new ArrayList<>();
    List<String> ans = new ArrayList<>();
    public List<List<String>> partition(String s) {
       backTracking(s,0);
       return resp;
    }

    public void backTracking(String s,int startIndex){
        if(startIndex >= s.length()){
            resp.add(new ArrayList<>(ans));
            return;
        }
        for(int i=startIndex;i<s.length();i++){
            if(isPanda(s,startIndex,i)){
               String str =  s.substring(startIndex,i+1);
               ans.add(str);
            }
            else{
                continue;
            }
            backTracking(s,i+1);
            ans.remove(ans.size()-1);
        }
    }

    //判断是否是否回文
    public boolean isPanda(String s,int start,int end){
        if(s.length() == 1){
            return true;
        }
      while(start < end){
          if(s.charAt(start) != s.charAt(end)){
              return false;
          }
          start++;
          end--;
      }
      return true;
    }
}
© gaohueric all right reserved,powered by Gitbook文件修订时间: 2021-12-08 23:22:22

results matching ""

    No results matching ""